package pl.druminski.robinhood.view;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetSocketAddress;
import java.net.Proxy;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 * @author Łukasz Drumiński
 * 
 */
public class DialogAddProxy extends JDialog implements ActionListener
{
    private JTextField fldIP = new JTextField(10);
    private JTextField fldPort = new JTextField(5);
    private Proxy.Type [] model = {Proxy.Type.HTTP, Proxy.Type.SOCKS};
    private JComboBox cmbType = new JComboBox(model);
    private JButton btnAddProxy = new JButton("Dodaj");
    
    private Proxy proxy = null;
    
    public Proxy getProxy()
    {
        return proxy;
    }

    public DialogAddProxy(Frame owner)
    {
        super(owner, true);
        this.setSize(new Dimension(500, 70));
        this.setTitle("Proxy");
        this.setLayout(new FlowLayout());
        this.setLocationRelativeTo(null);
        add(new JLabel("Adres: "));
        add(fldIP);
        add(new JLabel("Port: "));
        add(fldPort);
        add(new JLabel("Typ: "));
        add(cmbType);
        add(btnAddProxy);
        
        btnAddProxy.addActionListener(this);
        
    }

    public void actionPerformed(ActionEvent e)
    {
        if (fldIP.getText().length() > 6 
                && fldPort.getText().length()> 2
                && fldIP.getText().length()<=40)
        {
            int port = Integer.parseInt(fldPort.getText());
            String address = fldIP.getText();
            Proxy.Type proxyType = (Proxy.Type)cmbType.getSelectedItem();
            
            proxy = new Proxy(proxyType, new InetSocketAddress(address, port));
            this.setVisible(false);
            this.dispose();
            
        }
    }
}
